# @Version        : 1.0
# @Update Time    : 2025/3/15 12:56
# @File           : forgot_password_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
import flet as ft


class ForgotPasswordView(ft.Container):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.primary_color = "#4CAF50"
        self.input_border = "#BDBDBD"
        self.focus_color = "#388E3C"

        # 新增错误提示控件
        self.email_error = ft.Text(value="", color="red", visible=False)

        # 构建UI组件
        self.email_field = ft.TextField(
            label="邮箱地址",
            prefix_icon=ft.icons.EMAIL_OUTLINED,
            border_color=self.input_border,
            focused_border_color=self.focus_color,
            border_radius=5,
            height=50,
            text_size=14,
            content_padding=15,
            label_style=ft.TextStyle(color="#666666")
        )

        self.content = ft.Column(
            controls=[
                self._build_title(),
                self._build_description(),
                self._build_inputs(),
                self._build_submit_button()
            ],
            spacing=0,
            horizontal_alignment=ft.CrossAxisAlignment.CENTER,
        )

        # 容器样式
        self.width = 400
        self.padding = 30

    def _build_title(self):
        return ft.Column(
            [
                ft.Text(
                    "忘记密码",
                    size=24,
                    weight=ft.FontWeight.BOLD,
                    color=self.primary_color
                ),
                ft.Container(height=20)
            ], alignment=ft.MainAxisAlignment.CENTER
        )

    def _build_description(self):
        return ft.Column(
            [
                ft.Text(
                    "请您输入您的邮箱地址我们会将一个验证码发送到您的邮箱，以重置您的密码。",
                    size=12,
                    color="#666666",
                    text_align=ft.TextAlign.CENTER
                ),
                ft.Container(height=30)
            ],
            alignment=ft.MainAxisAlignment.CENTER
        )

    def _build_submit_button(self):
        return ft.ElevatedButton(
            content=ft.Text(
                "发送邮箱验证",
                color=ft.colors.WHITE,
                size=16
            ),
            bgcolor=self.primary_color,
            width=300,
            height=45,
            elevation=0,
            on_click=self.handle_submit,  # 修改为直接引用方法
            style=ft.ButtonStyle(
                shape=ft.RoundedRectangleBorder(radius=5),
                # padding={ft.ControlState.DEFAULT: 15}  # 修改为 ControlState
            )
        )

    def handle_submit(self, e):
        email = self.email_field.value
        self.email_error.visible = False  # 重置错误提示

        if not email:
            self.email_error.value = "请输入邮箱地址"
            self.email_error.visible = True
        else:
            self.email_error.visible = False
            self.page.show_snack_bar(
                ft.SnackBar(
                    content=ft.Text(f"已向 {email} 发送成功！"),  # 修改为包含邮箱账号
                    open=True
                )
            )
            self.page.go("/verification")  # 跳转到验证码校验界面
        self.update()

    def _build_inputs(self):
        return ft.Column(
            [
                self.email_field,
                self.email_error,  # 添加错误提示控件
                ft.Container(height=20)  # 增加间距
            ],
            spacing=15,
            alignment=ft.MainAxisAlignment.START  # 设置为靠左对齐
        )

